package com.cetc.baidumaptest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.DotOptions;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;

import java.util.ArrayList;

/**
 * Created by Young on 2017/1/12.
 */

public class RectangleOut extends AppCompatActivity {
    MapView mMapView = null;
    BaiduMap mBaiduMap = null;
    Integer clickNumber = 0;
    TextView txt1 = null;
    TextView txt2 = null;
    OverlayOptions ooPolyline;
    Polyline mPolyline;
    ArrayList<LatLng> line = new ArrayList<>();
    LatLng xPoint;
    LatLng yPoint;
    Boolean OUT = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
        //注意该方法要再setContentView方法之前实现
        SDKInitializer.initialize(getApplicationContext());
        final BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.icon);
        setContentView(R.layout.activity_line);
        txt1 = (TextView) findViewById(R.id.txt_point_1);
        txt2 = (TextView) findViewById(R.id.txt_point_2);
        mMapView = (MapView) findViewById(R.id.mapView);
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            public void onMapClick(LatLng point) {
                //在此处理点击事件
                OverlayOptions ooDot = new DotOptions().center(point).radius(10)
                        .color(0xFF00CD00);
                mBaiduMap.addOverlay(ooDot);
                clickNumber = clickNumber + 1;
                if (clickNumber == 1) {
                    mBaiduMap.clear();
                    line.add(0, point);
                    txt1.setText("纬度" + point.latitude + " 经度" + point.longitude);
                    xPoint = new LatLng(point.latitude, 0);
                    yPoint = new LatLng(0, point.longitude);
                } else if (clickNumber == 2) {
                    txt2.setText("纬度" + point.latitude + " 经度" + point.longitude);
                    xPoint = new LatLng(xPoint.latitude, point.longitude);
                    yPoint = new LatLng(point.latitude, yPoint.longitude);
                    line.add(1, xPoint);
                    line.add(2, point);
                    line.add(3, yPoint);
                    line.add(4, line.get(0));
                    //画线
                    ooPolyline = new PolylineOptions().width(15).points(line);
                    mPolyline = (Polyline) mBaiduMap.addOverlay(ooPolyline);
                } else if (clickNumber == 3) {
                    //描点
                    OverlayOptions options = new MarkerOptions()
                            .position(point)
                            .icon(bitmap);
                    mBaiduMap.addOverlay(options);
                    //判断
                    double x1 = line.get(0).latitude;
                    double y1 = line.get(0).longitude;
                    double x2 = line.get(2).latitude;
                    double y2 = line.get(2).longitude;
                    double x = point.latitude;
                    double y =point.longitude;
                    if ((x >= x1 && x <= x2 || x >= x2 && x <= x1)&&(y >= y1 && y <= y2 || y >= y2 && y <= y1)) {
                        OUT = false;
                    }else {
                        OUT = true;
                    }
                    if (OUT) {
                        Toast.makeText(getApplicationContext(), "范围外部", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "范围内部", Toast.LENGTH_LONG).show();
                    }
                    clickNumber = 0;
                    line.clear();
                }

            }

            public boolean onMapPoiClick(MapPoi poi) {
                //在此处理底图标注点击事件
                return false;
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }
}
